package cn.stylefeng.guns.modular.manager.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.ArchiveBorrow;
import cn.stylefeng.guns.modular.manager.entity.ArchiveBorrowDetail;
import cn.stylefeng.guns.modular.manager.mapper.ArchiveBorrowMapper;
import cn.stylefeng.guns.modular.manager.model.params.ArchiveBorrowParam;
import cn.stylefeng.guns.modular.manager.model.result.ArchiveBorrowResult;
import cn.stylefeng.guns.modular.manager.service.ArchiveBorrowDetailService;
import cn.stylefeng.guns.modular.manager.service.ArchiveBorrowService;
import cn.stylefeng.guns.sys.core.constant.state.YesNo;
import cn.stylefeng.guns.utils.ServiceAssert;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 档案借阅信息表 服务实现类
 * </p>
 *
 * @author blesslp
 * @since 2019-11-23
 */
@Service
public class ArchiveBorrowServiceImpl extends ServiceImpl<ArchiveBorrowMapper, ArchiveBorrow> implements ArchiveBorrowService {

    @Autowired
    private ArchiveBorrowDetailService archiveBorrowDetailService;

    @Override
    public boolean isBorrow(String[] daIds) {
        if (ToolUtil.isEmpty(daIds)) {
            return false;
        }
        final LambdaQueryWrapper<ArchiveBorrowDetail> wrapper = Wrappers.lambdaQuery();
        wrapper.isNull(ArchiveBorrowDetail::getGhsj);
        wrapper.in(ArchiveBorrowDetail::getDaid, daIds);
        return 0 != this.archiveBorrowDetailService.count(wrapper);
    }

    @Transactional
    @Override
    public void add(ArchiveBorrowParam param) {

        ArchiveBorrow entity = getEntity(param);
        //保存借阅信息
        entity.setSfgh(YesNo.NO.getCode());
        entity.setJysj(new Date());
        this.save(entity);
        //保存借阅详细信息
        if (!ToolUtil.isEmpty(param.getArchiveInfoIds())) {
            List<ArchiveBorrowDetail> details = new ArrayList<>();
            for (String daid : param.getArchiveInfoIds()) {
                ArchiveBorrowDetail detail = new ArchiveBorrowDetail();
                detail.setDaid(daid);
                detail.setDajyid(entity.getId());
                detail.setJysj(new Date());
                details.add(detail);
            }
            this.archiveBorrowDetailService.saveBatch(details);
        }
    }

    @Override
    public void delete(ArchiveBorrowParam param) {
        final Serializable key = getKey(param);
        boolean isSuccess = this.removeById(key);
        final String TIPS = "删除失败";
        ServiceAssert.assetTrue(isSuccess, TIPS);

        //移除所有关联表
        isSuccess = this.archiveBorrowDetailService.lambdaUpdate()
                .eq(ArchiveBorrowDetail::getDajyid, key)
                .remove();
    }

    @Override
    public void update(ArchiveBorrowParam param) {
        ArchiveBorrow oldEntity = getOldEntity(param);
        ArchiveBorrow newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public ArchiveBorrowResult findBySpec(ArchiveBorrowParam param) {
        return null;
    }

    @Override
    public List<ArchiveBorrowResult> findListBySpec(ArchiveBorrowParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(ArchiveBorrowParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(ArchiveBorrowParam param) {
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private ArchiveBorrow getOldEntity(ArchiveBorrowParam param) {
        return this.getById(getKey(param));
    }

    private ArchiveBorrow getEntity(ArchiveBorrowParam param) {
        ArchiveBorrow entity = new ArchiveBorrow();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
